function checkAlphaNumeric(txt){
	  return (txt.match( /[A-Za-z][A-Za-z0-9]*/ ) == txt);
}

function checkNumeric(txt){
	  return (txt.match( /[0-9]+/ ) == txt);
}

function checkAlphabetic(txt){
	  return (txt.match( /[A-Za-z]+/ ) == txt);
}

//Register click handler  
$(document).ready(function(){
  $("#updateBtn").click(function(){
    	var can_update = true;
		//checking sur name
		if (!checkAlphabetic(document.profileForm.surName.value)){
			$("#surNameErr").html("Must be alphabetic!");
			can_update = false;
		}else{
			$("#surNameErr").html("");
		}
		
		//checking given name
		if (!checkAlphabetic(document.profileForm.givenName.value)){
			$("#givenNameErr").html("Must be alphabetic!");
			can_update = false;
		}else{
			$("#givenNameErr").html("");
		}
		
		//checking phone number
		if (!checkNumeric(document.profileForm.phoneNumber.value)){
			$("#phoneNumberErr").html("Must be numeric!");
			can_update = false;
		}else{
			if((document.profileForm.phoneNumber.value.length < 9) || (document.profileForm.phoneNumber.value.length > 10)){
				$("#phoneNumberErr").html("Must be 9 or 10 characters");
    			can_update = false;    				
			}else{
				$("#phoneNumberErr").html("");
			}
		}
		
		//checking password
		if(document.profileForm.password1.value != ""){
			if (!checkAlphaNumeric(document.profileForm.password1.value)){
				$("#password1Err").html("Must be alpha-numeric!");
				can_update = false;
			}else{
				if((document.profileForm.password1.value.length < 5) || (document.profileForm.password1.value.length > 25)){
					$("#password1Err").html("Must be between 5 or 25 characters");
	    			can_update = false;    				
				}else{
					$("#password1Err").html("");
				}
			}
		}
		
		if (document.profileForm.password1.value != document.profileForm.password2.value){
			$("#password2Err").html("Passwords don't match");
			can_update = false;
		}else{
			$("#password2Err").html("");
		}
		
		if (can_update){
    		$.post("profile", $("#profileForm").serialize(),function(data){
    			//making it work with FF......
    			var serializer = new XMLSerializer();
    			var xml = serializer.serializeToString(data);
    			data = xml.replace("<firefoxsucks>","").replace("</firefoxsucks>","");
    			//real begining fo function
    			if (data == "updated"){
    				//do the same as when 'home' is clicked
    				$("#result").html("Updated successfully");
    				$("#result").css("color","#00AA00");
    				
    			}else{
    				$("#result").html("Failed to update");
    				$("#result").css("color","#FF0000");
    			}
    		});
		}else{
			$("#result").html("");
		}    	
	});
});
  
